/*
 * @lc app=leetcode.cn id=355 lang=javascript
 *
 * [355] 设计推特
 */

// @lc code=start
/**
 * Initialize your data structure here.
 */
var Twitter = function() {
  this.userMap = new Map();
};

/**
 * Compose a new tweet. 
 * @param {number} userId 
 * @param {number} tweetId
 * @return {void}
 */
Twitter.prototype.postTweet = function(userId, tweetId) {
  if(!this.userMap.has(userId)){
    this.userMap.set(userId, new User(userId));
  }
  let u = this.userMap.get(userId);
  u.post(tweetId);
};

/**
 * Retrieve the 10 most recent tweet ids in the user's news feed. Each item in the news feed must be posted by users who the user followed or by the user herself. Tweets must be ordered from most recent to least recent. 
 * @param {number} userId
 * @return {number[]}
 */
Twitter.prototype.getNewsFeed = function(userId) {
  var h = new Heap();
  var res = [], candidates = [];
  if(!this.userMap.has(userId)){
    return res;
  }
  for (let ids of this.userMap.get(userId).followed) {
    candidates = candidates.contact(this.userMap.get(ids).tweets)
  }
  // 根据时间调整堆 
  h.build(candidates, 'time'); 
  // 根据时间拿出最新的⼗条推特 
  while (res.length < 10 && h.data.length) { 
    res.push(h.deleting('time').tweetId); 
  } 
  return res;
};

/**
 * Follower follows a followee. If the operation is invalid, it should be a no-op. 
 * @param {number} followerId 
 * @param {number} followeeId
 * @return {void}
 */
Twitter.prototype.follow = function(followerId, followeeId) {

};

/**
 * Follower unfollows a followee. If the operation is invalid, it should be a no-op. 
 * @param {number} followerId 
 * @param {number} followeeId
 * @return {void}
 */
Twitter.prototype.unfollow = function(followerId, followeeId) {

};

/**
 * Your Twitter object will be instantiated and called as such:
 * var obj = new Twitter()
 * obj.postTweet(userId,tweetId)
 * var param_2 = obj.getNewsFeed(userId)
 * obj.follow(followerId,followeeId)
 * obj.unfollow(followerId,followeeId)
 */
let timeStamp = 0;
var Tweet = function(tweetId, timeStamp){
  this.tweetId = tweetId;
  this.timeStamp = timeStamp;
}

var User = function(userId){
  this.id = userId;
  this.followd = new Set();
  this.twitters = [];
  this.follow(userId)
}
User.prototype.follow = function(userId){
  this.followed.add(userId);
}
User.prototype.unFollow = function(userId){
  this.followed.delete(userId);
}
User.prototype.post = function(tweetId){
  var tweet = new Tweet(tweetId, timeStamp);
  timeStamp++;
  this.twitters.unshift(tweet);
}

function Heap(){

}

// @lc code=end